package ta.dao;

import java.util.ArrayList;
import java.util.List;

import ta.pojo.Blog;
import ta.utils.JDBCUtils;
import ta.utils.PageInfo;

public class BlogDao {
    public<T> void getAll(String keyword,PageInfo<Blog> pb) {
        String sql1="select count(*) from blog where  blogid like  concat('%',?,'%') or title like  concat('%',?,'%') or content like  concat('%',?,'%') or author like concat('%',?,'%')";
        Object[] params1= {keyword,keyword,keyword,keyword};
        long total=(long) JDBCUtils.getScalar(sql1, params1);
        int totalpage=(int)(total+pb.getPagesize()-1)/pb.getPagesize();
        String sql="select * from blog where  blogid like  concat('%',?,'%') or title like  concat('%',?,'%') or content like  concat('%',?,'%') or author like concat('%',?,'%') order by pubtime desc"
                + " limit ?,?";
        Object[] params= {keyword,keyword,keyword,keyword,(pb.getCurrentpage()-1)*pb.getPagesize(),pb.getPagesize()};
        pb.setTotalpage(totalpage);
        pb.setData(JDBCUtils.getList(sql, params,Blog.class));
    }
    public <T> List<Blog> getAll(String keyword) {
        String sql="select * from blog where  author=? order by pubtime desc";
        Object[] params= {keyword};
        return JDBCUtils.getList(sql, params,Blog.class);
    }
    public<T> List<Blog> getAll() {
        String sql="select blogid,title,content,category,author,pubtime from blog order by pubtime desc";
        return JDBCUtils.getList(sql,Blog.class);
    }
    public<T> void getAll(String keyword,String category,PageInfo<Blog> pb) {
        String sql1="select count(*) from blog where category=?  ";
        Object[] params1= {category};
        long total=(long) JDBCUtils.getScalar(sql1, params1);
        int totalpage=(int)(total+pb.getPagesize()-1)/pb.getPagesize();
        String sql="select * from blog where category=? order by pubtime desc"
                + " limit ?,?";
        Object[] params= {category,(pb.getCurrentpage()-1)*pb.getPagesize(),pb.getPagesize()};
        pb.setTotalpage(totalpage);
        pb.setData(JDBCUtils.getList(sql, params,Blog.class));
    }

    public Blog getById(String blogid) {
        String sql="select * from blog where blogid=? ";
        Object[] params= {blogid};
        return JDBCUtils.getBean(sql,params, Blog.class);
    }

    public boolean add(Blog blog) {
        String sql="insert into blog(title,content,pubtime,author,category) values(?,?,?,?,?) ";
        Object[] params= {blog.getTitle(),blog.getContent(),blog.getPubtime(),blog.getAuthor(),blog.getCategory()};
        return JDBCUtils.update(sql, params);
    }

    //删除或批量删除（用,间隔id）
    public boolean del(String ids) {
        String[] params=ids.split(",");
        String sql="delete from blog where blogid in (";
        for(String k:params)
        {
            sql+="?,";
        }
        sql=sql.substring(0,sql.length()-1);
        sql+=")";
        return JDBCUtils.update(sql, params);
    }

    public boolean  edit(Blog blog) {
        List<Object> list=new ArrayList<Object>();
        StringBuilder sql=new StringBuilder("update blog  set ");
        if(blog.getTitle()!=null)
        {
            sql.append("title=?,");
            list.add(blog.getTitle());
        }
        if(blog.getContent()!=null)
        {
            sql.append("content=?,");
            list.add(blog.getContent());
        }
        if(blog.getPubtime()!=null)
        {
            sql.append("pubtime=?,");
            list.add(blog.getPubtime());
        }
        if(blog.getCategory()!=null)
        {
            sql.append("category=?,");
            list.add(blog.getCategory());
        }
        sql.deleteCharAt(sql.length()-1);
        sql.append("  where blogid=?");
        list.add(blog.getBlogid());
        return JDBCUtils.update(sql.toString(), list.toArray());
    }

//    public static void main(String[] args) {
//        BlogDao dao=new BlogDao();
//        String keyword="1212";
//        List<Blog> blog=dao.getAll(keyword);
//        System.out.println(blog.size());
//
//    }

//    public static void main(String[] args) {
//        BlogDao dao=new BlogDao();
//        Blog blog=new Blog();
//
//        blog.setTitle("ccc");
//        blog.setContent("bbb");
//        LocalDateTime date=LocalDateTime.now();
//        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        blog.setPubtime(date.format(dtf));
//        blog.setAuthor("bbb");
//        blog.setCategory("bbb");
//        dao.add(blog);
//        blog.setBlogid(12);
//        dao.edit(blog);
//        Blog b=dao.getById("6");
//        System.out.println(b.getTitle());
//        System.out.println(b.getCategory());


        //dao.del("10");


//		Newsinfo newsinfo=dao.getById("1");
//		System.out.println(newsinfo.getTitle());
//


//        PageInfo<Blog> pb=new PageInfo<Blog>();
//        dao.getAll("", "",pb);
//        System.out.println(pb.getData());
//  }


}
